import Vue from 'vue'
import Router from 'vue-router'
import {isAuthenticated} from '../utils/auth'
Vue.use(Router)

export const constantRouterMap = [
  {
    path:'/',
    name: "Login",
    component: resolve => require(['@/views/login/login.vue'], resolve),
    meta: { title: '后台登录' }
  },
  {
    path: '/home',
    name: 'Home',
    component: resolve => require(['@/views/home/home.vue'], resolve),
    beforeEnter: (to, from, next) => {
      // ...
      if(to.name !== 'Login' && !isAuthenticated()){
        next({name:'Login'})
      }
      else{
        next()
      }
    },
    meta: { title: '主页' },
    children: [
      {
        path: '/home/bookmanage',
        component: resolve => require(['@/components/table/booktable.vue'], resolve),
        meta: { title: '图书管理' },
        beforeEnter: (to, from, next) => {
          // ...
          if(to.name !== 'Login' && !isAuthenticated()){
            next({name:'Login'})
          }
          else{
            next()
          }
        }
      },
      {
        path: '/home/catemanage',
        component: resolve => require(['@/components/table/categorytable.vue'], resolve),
        meta: { title: '分类管理' },
        beforeEnter: (to, from, next) => {
          // ...
          if(to.name !== 'Login' && !isAuthenticated()){
            next({name:'Login'})
          }
          else{
            next()
          }
        }
      },
      {
        path: '/home/stockmanage',
        component: resolve => require(['@/components/table/stocktable.vue'], resolve),
        meta: { title: '库存管理' },
        beforeEnter: (to, from, next) => {
          // ...
          if(to.name !== 'Login' && !isAuthenticated()){
            next({name:'Login'})
          }
          else{
            next()
          }
        }
      },
      {
        path: '/home/ordermanage',
        component: resolve => require(['@/components/table/ordertable.vue'], resolve),
        meta: { title: '订单管理' },
        beforeEnter: (to, from, next) => {
          // ...
          if(to.name !== 'Login' && !isAuthenticated()){
            next({name:'Login'})
          }
          else{
            next()
          }
        }
      },
      {
        path: '/home/activitymanage',
        component: resolve => require(['@/components/table/activitytable.vue'], resolve),
        meta: { title: '活动管理' },
        beforeEnter: (to, from, next) => {
          // ...
          if(to.name !== 'Login' && !isAuthenticated()){
            next({name:'Login'})
          }
          else{
            next()
          }
        }
      },
      {
        path: '/home/usermanage',
        component: resolve => require(['@/components/table/usertable.vue'], resolve),
        meta: { title: '用户管理' },
        beforeEnter: (to, from, next) => {
          // ...
          if(to.name !== 'Login' && !isAuthenticated()){
            next({name:'Login'})
          }
          else{
            next()
          }
        }
      }
    ]
  },
{ path: '*', redirect: '/404', hidden: true }
  ]
  

  export default new Router({
    // mode: 'history', //后端支持可开
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRouterMap
  })
  